package com.sugon.config;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.sugon.service.SysConfigService;
import com.sugon.utils.ConfigConstant;
import com.sugon.utils.MinioUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author YuXD
 * @Version 1.0
 * @since 2022-11-01
 */
@Slf4j
@Configuration
public class MinIoClientConfig {

    @Resource
    private SysConfigService configService;

    /**
     * 注入minio 客户端
     *
     * @return
     */
    @Bean
    public String minioClient() {
        // 获取minin客户端配置
        String minioConfig = configService.getMinioConfig();
        List<String> minioConfigList = StrUtil.split(minioConfig, ",");
        Assert.isTrue(minioConfigList.size() == 4);
        String minioUrl = minioConfigList.get(0);
        String minioName = minioConfigList.get(1);
        String minioPass = minioConfigList.get(2);
        String bucketName = minioConfigList.get(3);

        if (!minioUrl.startsWith(ConfigConstant.STR_HTTP)) {
            minioUrl = "http://" + minioUrl;
        }
        if (!minioUrl.endsWith(ConfigConstant.SINGLE_SLASH)) {
            minioUrl = minioUrl.concat(ConfigConstant.SINGLE_SLASH);
        }
        MinioUtil.setMinioUrl(minioUrl);
        MinioUtil.setMinioName(minioName);
        MinioUtil.setMinioPass(minioPass);
        MinioUtil.setBucketName(bucketName);
        log.info("初始化minio完毕");
        return "初始化minio完毕";
    }

}
